package com.uchonor.api.core.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.uchonor.api.core.entity.InterfaceConfig;
import com.uchonor.api.core.service.InterfaceConfigService;
import com.uchonor.olight.agent.annotation.Server;
import com.uchonor.olight.agent.annotation.web.*;
import com.uchonor.olight.agent.module.web.BaseDatagridController;
import com.uchonor.olight.agent.module.web.vo.RspResult;
import com.uchonor.olight.agent.module.web.vo.ServiceResult;
import com.uchonor.olight.agent.module.web.vo.StatusCode;

@Server(path = "/interfaceConfig")
public class InterfaceConfigController extends BaseDatagridController<InterfaceConfigService, InterfaceConfig> {

    /**
     * 根据groupId获取对应的接口信息
     * @param groupId
     * @return
     */
    @Get(path = "/getListByGroupId")
    public ServiceResult getListByGroupId(@RequestParam(name = "groupId") String groupId){
        ServiceResult sr = new ServiceResult();
        RspResult rr = new RspResult();
        try {
            QueryWrapper<InterfaceConfig> queryWrapper = Wrappers.query();
            queryWrapper.eq("group_id",groupId);
            queryWrapper.orderByDesc("update_time");
            rr.setDATA(this.baseService.list(queryWrapper));
        } catch (Exception e) {
            rr.setRSP_DESC(e.getMessage());
            rr.setRSP_CODE(StatusCode.CODE_3000.getKey());
        }
        sr.setRSP(rr);
        return sr;
    }

    @Post(path = "/saveInterfaceConfig")
    public ServiceResult saveInterfaceConfig(@RequestBody InterfaceConfig interfaceConfig){
        ServiceResult sr = new ServiceResult();
        RspResult rr = new RspResult();
        try {
            setCreateUser(interfaceConfig);
            rr.setDATA(this.baseService.saveOrUpdate(interfaceConfig));
        } catch (Exception e) {
            rr.setRSP_DESC(e.getMessage());
            rr.setRSP_CODE(StatusCode.CODE_3000.getKey());
        }
        sr.setRSP(rr);
        return sr;
    }

    @Delete(path = "/deleteInterfaceConfig")
    public ServiceResult deleteInterfaceConfig(@RequestParam(name="interfaceId") String interfaceId){
        ServiceResult sr = new ServiceResult();
        RspResult rr = new RspResult();
        try {
            rr.setDATA(this.baseService.removeById(interfaceId));
        } catch (Exception e) {
            rr.setRSP_DESC(e.getMessage());
            rr.setRSP_CODE(StatusCode.CODE_3000.getKey());
        }
        sr.setRSP(rr);
        return sr;
    }

    @Post(path = "/effectiveInterfaceConfig")
    public ServiceResult effectiveInterfaceConfig(@RequestParam(name="interfaceId") String interfaceId){
        ServiceResult sr = new ServiceResult();
        RspResult rr = new RspResult();
        try {
            this.baseService.effectiveInterfaceConfig(interfaceId);
        } catch (Exception e) {
            rr.setRSP_DESC(e.getMessage());
            rr.setRSP_CODE(StatusCode.CODE_3000.getKey());
        }
        sr.setRSP(rr);
        return sr;
    }

    @Post(path = "/invalidInterfaceConfig")
    public ServiceResult invalidInterfaceConfig(@RequestParam(name="interfaceId") String interfaceId){
        ServiceResult sr = new ServiceResult();
        RspResult rr = new RspResult();
        try {
            this.baseService.invalidInterfaceConfig(interfaceId);
        } catch (Exception e) {
            rr.setRSP_DESC(e.getMessage());
            rr.setRSP_CODE(StatusCode.CODE_3000.getKey());
        }
        sr.setRSP(rr);
        return sr;
    }

}
